package com.hllxd.hism.star.basic.service.service;

import com.hllxd.hism.star.basic.service.dao.mapper.mis.DataAdtypeDefineMapper;
import com.hllxd.hism.star.basic.service.model.TblDataAdtypeDefine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service("AdtypeDefineService")
public class AdtypeDefineServiceImpl implements AdtypeDefineService {

    @Autowired
    private DataAdtypeDefineMapper dataAdtypeDefineMapper;

    @Override
    public List<TblDataAdtypeDefine> listAdTypeDefine() {
//        TblDataAdtypeDefineExample defineExample = new TblDataAdtypeDefineExample();
//        TblDataAdtypeDefineExample.Criteria criteria = defineExample.createCriteria();
//        criteria.andActionIn(Arrays.asList(new Byte[]{0, 1}));
//        return dataAdtypeDefineMapper.selectByExample(defineExample);
//
        return dataAdtypeDefineMapper.selectByAll();
    }

    @Override
    public Long setAdTypeDefine(TblDataAdtypeDefine aa) {
        long id;
        if (aa.getId() != null && aa.getId() > 0) {
            aa.setCreator(null);
            id = dataAdtypeDefineMapper.updateByPrimaryKeySelective(aa);
        } else {
            id = dataAdtypeDefineMapper.insertSelective(aa);
        }
        return id;
    }

    @Override
    public Long delAdtypeDefine(Long id, String userName) {
        TblDataAdtypeDefine aa = new TblDataAdtypeDefine();
        aa.setId(id);
        aa.setAction((byte) 2);
        aa.setLastOperator(userName);
        return dataAdtypeDefineMapper.updateByPrimaryKeySelective(aa);
    }

    @Override
    public TblDataAdtypeDefine getAdtypeDefine(Long id) {
        return dataAdtypeDefineMapper.selectByPrimaryKey(id);
    }

    @Override
    public Long updateValid(TblDataAdtypeDefine entity) {
        TblDataAdtypeDefine aa = new TblDataAdtypeDefine();
        aa.setId(entity.getId());
        aa.setIsValid(entity.getIsValid());
        aa.setLastOperator(entity.getLastOperator());
        if (entity.getIsValid() != null && entity.getIsValid() == (byte) 1) {
            aa.setValidTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        return dataAdtypeDefineMapper.updateByPrimaryKeySelective(aa);
    }
}
